<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login Page</title>
    <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }
        .login-container {
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            width: 300px;
        }
        .login-container h2 {
            text-align: center;
            margin-bottom: 20px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        .form-group label {
            display: block;
            margin-bottom: 5px;
        }
        .form-group input {
            width: 100%;
            padding: 8px;
            box-sizing: border-box;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
        .btn {
            width: 100%;
            padding: 10px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        .btn:hover {
            background-color: #0056b3;
        }
        .forgot-password {
            text-align: center;
            margin-top: 10px;
        }
        .forgot-password a {
            color: #007bff;
            text-decoration: none;
        }
        .forgot-password a:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
<div class="login-container">
    <h2>学生登录</h2>
    <form id="loginForm">
        <div class="form-group">
            <label for="username">账号</label>
            <input type="text" id="username" name="username" required>
        </div>
        <div class="form-group">
            <label for="password">密码</label>
            <input type="password" id="password" name="password" required>
        </div>
    </form>
    <div class="form-group">
        <p id="tip" style="color: red"></p>
    </div>
    <div class="form-group">
        <button class="btn" onclick="subLogin()">登录</button>
    </div>
    <div class="forgot-password">
        <a href="/register">注册</a>
    </div>
</div>

</body>
</html>
<script type="application/javascript">
    function subLogin(){
        console.log("--------------")
        //alert("账号和密码不能为空")
        // 获取用户填写的账号和密码
        let account = document.getElementById("username").value
        let password = document.getElementById("password").value

        //判断用户填写的账号是否为空
        if ("" == account || "" == password){
            // alert('账号或密码不能为空');
            document.getElementById("tip").innerHTML = "账号或密码不能为空"
            return
        }
        //将用户的账号和密码发送给后端验证是否正确，正确就跳转刀系统的首页，错误的话就停在当前页面，并且告诉用户账号或密码错误
        $.ajax({
            url: "/doLogin", //后端的请求地址
            type: "POST", //请求的方式
            data: { //表示前端给后端的请求参数
                username: account,
                password: password
            },//请求的参数
            dataType: "text",//后端返回的数据类型
            success: function (res) {//请求完毕的回调函数
                console.log('res',res)
                if (res != null){
                    //登录成功
                    //保存当前登录成功的用户
                    alert("登陆成功")
                    localStorage.setItem("username",res);
                    //将页面重定向到系统首页
                    window.location.href = "/dormitory"
                }else {
                    document.getElementById("tip").innerHTML = "账号或密码错误"
                    return
                }
            }
        })
    }

</script>